package com.tgh.gles2.c5.sphereworld;

import com.tgh.gles2.R;
import com.tgh.gles2.c4.sphereworld.SphereWorldRenderer;
import com.tgh.utils.GLBatch;
import com.tgh.utils.GLShaderManager.ShaderType;
import com.tgh.utils.GLTools;
import com.tgh.utils.Math3D;
import com.tgh.utils.Matrix44f;
import com.tgh.utils.Vector4f;
import android.content.Context;
import android.opengl.GLES20;
import android.os.SystemClock;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class SphereWorldTextureRenderer extends SphereWorldRenderer {

    public SphereWorldTextureRenderer(Context context) {
        super(context);
        initFloor();
    }
    
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        super.onSurfaceCreated(gl, config);
        initTextures();
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT|GLES20.GL_DEPTH_BUFFER_BIT);
        modelviewMatrix.pushMatrix();
            Matrix44f cameraMatrix = cameraFrame.getCameraMatrix(false);
            modelviewMatrix.multMatrix(cameraMatrix);
            
            float yRot=SystemClock.elapsedRealtime()/1000f*60.0f;
            modelviewMatrix.pushMatrix();
                modelviewMatrix.scale(1, -1, 1);
                modelviewMatrix.translate(0, 0.8f, 0);
                GLES20.glFrontFace(GLES20.GL_CW);
                drawSongAndDance(yRot);
                GLES20.glFrontFace(GLES20.GL_CCW);
            modelviewMatrix.popMatrix();
            
            GLES20.glEnable(GLES20.GL_BLEND);
            GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
            shaderManager.useStockShader(ShaderType.TEXTURE_MODULATE, 
                    transformPipleline.getModelViewProjectionMatrix().getArray(),
                    floorColor,0);
            floorBatch.draw();
            GLES20.glEnable(GLES20.GL_BLEND);
            drawSongAndDance(yRot);
        modelviewMatrix.popMatrix();
    }
    
    protected void drawSongAndDance(float yRot) {
        Matrix44f matrix = modelviewMatrix.getMatrix();
        Vector4f lightPosTransformed=new Vector4f();
        Math3D.transformVector4(lightPosTransformed, lightPos, matrix);
        
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[2]);
        for(int i=0;i<spheres.length;i++){
            modelviewMatrix.pushMatrix();
                modelviewMatrix.multMatrix(spheres[i]);
                shaderManager.useStockShader(ShaderType.TEXTURE_POINT_LIGHT_DIFF, 
                        transformPipleline.getModelViewMatrix().getArray(),
                        transformPipleline.getProjectionMatrix().getArray(),
                        lightPosTransformed.toArray(),
                        whiteColor,
                        0
                        );
                sphereBatch.draw();
            modelviewMatrix.popMatrix();
        }
        
        modelviewMatrix.translate(0, 0.2f, -2.5f);
        modelviewMatrix.pushMatrix();
            modelviewMatrix.rotate(yRot, 0, 1, 0);
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[1]);
            shaderManager.useStockShader(ShaderType.TEXTURE_POINT_LIGHT_DIFF, 
                    transformPipleline.getModelViewMatrix().getArray(),
                    transformPipleline.getProjectionMatrix().getArray(),
                    lightPosTransformed.toArray(),
                    whiteColor,
                    0
                    );
            torusBatch.draw();
        modelviewMatrix.popMatrix();
        
        modelviewMatrix.rotate(yRot*-2.0f, 0, 1, 0);
        modelviewMatrix.translate(0.8f, 0, 0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[2]);
        shaderManager.useStockShader(ShaderType.TEXTURE_POINT_LIGHT_DIFF, 
                transformPipleline.getModelViewMatrix().getArray(),
                transformPipleline.getProjectionMatrix().getArray(),
                lightPosTransformed.toArray(),
                whiteColor,
                0
                );
        sphereBatch.draw();
    }
    
    private void initFloor() {
        float texSize=10.0f;
        floorBatch=new GLBatch(GLES20.GL_TRIANGLE_FAN, 4);
        
        floorBatch.multiTexCoord(0, 0, 0);
        floorBatch.vertex(-20, -0.41f, 20);
        
        floorBatch.multiTexCoord(0, texSize, 0);
        floorBatch.vertex(20, -0.41f, 20);
        
        floorBatch.multiTexCoord(0, texSize, texSize);
        floorBatch.vertex(20, -0.41f, -20);
        
        floorBatch.multiTexCoord(0, 0, texSize);
        floorBatch.vertex(-20, -0.41f, -20);
        
        floorBatch.end();
    }
    
    private  void initTextures() {
        textures = GLTools.genTextures(3);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
        GLTools.loadDrawableToTexture2D(mContext, R.drawable.marble, 
                GLES20.GL_LINEAR_MIPMAP_LINEAR, GLES20.GL_LINEAR, GLES20.GL_REPEAT, true, true);
        
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[1]);
        GLTools.loadDrawableToTexture2D(mContext, R.drawable.marslike, 
                GLES20.GL_LINEAR_MIPMAP_LINEAR, GLES20.GL_LINEAR, GLES20.GL_CLAMP_TO_EDGE, true, true);
        
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[2]);
        GLTools.loadDrawableToTexture2D(mContext, R.drawable.moonlike, 
                GLES20.GL_LINEAR_MIPMAP_LINEAR, GLES20.GL_LINEAR, GLES20.GL_CLAMP_TO_EDGE, true, true);
    }
    
    protected GLBatch floorBatch;
    protected float[] whiteColor=new float[]{1,1,1,1};
    protected float[] floorColor=new float[]{1,1,1,0.75f};
    protected Vector4f lightPos=new Vector4f(0, 3, 0, 1);
    private int[] textures;

}
